#include <stdio.h>
#include <iostream>
#include <cstring>
#include <mysql/mysql.h>
using namespace std;
int main(int argc,char *argv[])
{
    MYSQL conn;
    
	MYSQL_RES * result;
	MYSQL_ROW row;

    int res,res2,res3;
    char *a="renziyiu";
    char *b="zi";
    char *c="z";
    char *sqlinsert = new char[100];
    char *sqldelete = new char[100];
    char *sqlupdate = new char[100];
    char *sqlquery = new char[100];
     strcpy(sqlinsert, "insert into test(name) values('");
     strcat(sqlinsert,a);
     strcat(sqlinsert,"')");
     cout<<sqlinsert<<endl;
     
     strcpy(sqldelete,"delete from test where name='");
     strcat(sqldelete,b);
     strcat(sqldelete,"'");
     
     strcpy(sqlquery,"select * from test where name='");
     strcat(sqlquery,c);
     strcat(sqlquery,"'");
     
     strcpy(sqlupdate,"update test set name='rzy' where name='");
     strcat(sqlupdate,a);
     strcat(sqlupdate,"'");
     
    mysql_init(&conn);
    //"root":数据库管理员 "song":root密码 "test":数据库的名字
    if(mysql_real_connect(&conn, "localhost","root","song","test",0,NULL,CLIENT_FOUND_ROWS))
    {
        cout << "connect success" << endl;
       // res = mysql_query(&conn, "insert into test(name) values('s')");
         res = mysql_query(&conn,sqlinsert);
          res2 = mysql_query(&conn,sqldelete);
           res3 = mysql_query(&conn,sqlupdate);
    if(res)  //res=1,error
    {
        printf("error\n");
      
    }
    else
    {
        printf("OK\n");
    }
    
    if (mysql_query(&conn, sqlquery) != 0) {
	fprintf(stderr, "查询失败！\n");
	exit(1);
	}
    else {
	if ((result = mysql_store_result(&conn)) == NULL) {
	        fprintf(stderr, "保存结果集失败！\n");
		exit(1);
		}
	else {
	while ((row = mysql_fetch_row(result)) != NULL) {
		cout<<"id: "<<row[0]<<endl;		
		printf("id is %d , ", atoi(row[0]));  //查询结果都是字符型的，对于ID也是，需要转为整型
		printf("name is %s\n", row[1]);
		}
	}
	
	mysql_free_result(result);     //释放结果集所占用的内存
	}

    mysql_close(&conn);
    }else
    {
        cout << "connect failed" << endl;
    }
    
    
    
    return 0;
}